using Furion;
using Furion.Localization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Threading.Tasks;

namespace WebApplication1
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers().AddInject();

            // 添加多语言配置选项

            //// 添加多语言配置选项
            //services.AddConfigurableOptions<LocalizationSettingsOptions>();

            //// 获取多语言配置选项
            //var localizationSettings = App.GetConfig<LocalizationSettingsOptions>("LocalizationSettings", true);

            //// 注册多语言服务
            //services.AddLocalization(options =>
            //{
            //    if (!string.IsNullOrWhiteSpace(localizationSettings.ResourcesPath))
            //        options.ResourcesPath = localizationSettings.ResourcesPath;
            //});

            //services.AddConfigurableOptions<LocalizationSettingsOptions>();
            //services.AddJsonLocalization(options => { options.ResourcesPath = "Resources"; });

            services.AddConfigurableOptions<LocalizationSettingsOptions>();
            var localizationSettings = App.GetConfig<LocalizationSettingsOptions>("LocalizationSettings", true);
            services.AddJsonLocalization(options => { options.ResourcesPath = localizationSettings.ResourcesPath; });
            services.AddMvc()
                 .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                 .AddDataAnnotationsLocalization(options =>
                 {
                     options.DataAnnotationLocalizerProvider = (type, factory) =>
                         factory.Create(localizationSettings.LanguageFilePrefix, localizationSettings.AssemblyName);
                 });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // 配置多语言，必须在 路由注册之前
            app.UseAppLocalization();

            //var localizationSettings = app.ApplicationServices.GetRequiredService<IOptions<LocalizationSettingsOptions>>().Value;
            //var supportcultures = new List<CultureInfo>();
            //foreach (var item in localizationSettings.SupportedCultures)
            //{
            //    supportcultures.Add(new CultureInfo(item));
            //}
            //app.UseRequestLocalization(new RequestLocalizationOptions
            //{
            //    DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture(localizationSettings.DefaultCulture),
            //    SupportedCultures = supportcultures,
            //    SupportedUICultures = supportcultures
            //});



            app.UseRouting();

            app.UseAuthorization();

            // 添加这一行，如果是 MVC和API共存项目，无需添加 string.Empty
            app.UseInject(string.Empty);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
